#include "StdAfx.h"
#include "Read.h"

CRead::CRead(void)
: m_pConfig(NULL)
{
	
}

CRead::~CRead(void)
{

	
}
int CRead::SetAdsMap(void)
{
	if (m_pConfig == NULL)
	{
		return -1;
	}
	for (unsigned int i = 0; i < (m_pConfig->uiCount); i++)
	{
		CString strText;
		CString strUrl;
		strText.Format("%s",m_pConfig[i].pTextBuf);
		strUrl.Format("%s", m_pConfig[i].pUrlBuf);
		m_oAdMap.SetAt(strText,strUrl);
	}
	return 0;
}
int CRead::GetAdsCount(void)
{
	return (m_pConfig->uiCount);
}
bool CRead::ReadConfig()
{
	CStdioFile file;
	CString strBuf;
	if (!file.Open("../debug/Config/config.ini", CStdioFile::modeRead))
	{
		return false;
	}
	while (file.ReadString(strBuf))
	{
		int iIndex = strBuf.Find("ADVISECOUNT");
		if (-1!= iIndex)
		{
			strBuf.Delete(0,iIndex+strlen("ADVISECOUNT")+1);
			int iCount = atoi(strBuf.GetBuffer());
			m_pConfig = new ASDCONFIG[iCount];
			if (m_pConfig == NULL)
			{
				return false;
			}
			int iSub = 0;
			while (file.ReadString(strBuf))
			{
				m_pConfig[iSub].uiCount = iCount;
				iIndex = strBuf.Find("|");
				if (-1 == iIndex)
				{
					iIndex = strBuf.Find("[");
					if (-1 != iIndex)
					{
						file.Close();
						SetAdsMap();
						return true;
					}
					continue;
				}
				else
				{
					iIndex = strBuf.Find("=");
					strBuf.Delete(0,iIndex+1);
					CString strCur = strBuf;
					iIndex = strBuf.Find("|");
					memcpy(m_pConfig[iSub].pTextBuf,strBuf.GetBuffer(), iIndex);
					strBuf.Delete(0, iIndex+1);

					iIndex = strBuf.Find("|");
					strncpy(m_pConfig[iSub].pUrlBuf, strBuf.GetBuffer(), iIndex);
					strBuf.Delete(0, iIndex+1);

					CString strCur1 = strBuf;
					CString strBuf2 = strBuf;
					iIndex = strBuf.Find("|");
					strBuf2.Delete(iIndex, strBuf2.GetLength()-iIndex);
					strCur1.Delete(0, iIndex+1);
					m_pConfig[iSub].uiSecond = atoi(strBuf2.GetBuffer());
					m_pConfig[iSub].color = atoi(strCur1.GetBuffer());

					iSub++;

				}

			}
		}
		
	}
	return false;

}

int CRead::DeleteStruct()
{
	if (m_pConfig)
	{
		//int iSum = m_pConfig->uiCount;
		//for (int i = 0; i< iSum; i++)
		//{
		//	ASDCONFIG *pData = m_pConfig[i];
		//	if (pData)
		//	{
		//		delete pData;
		//	}
		//	pData = NULL;
		//}
		
	}
	return 0;
}